package ui.activity.user;

import ui.model.user.UserRegisterViewModel;
import util.web.WebServiceHelper;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import business.entity.User;
import business.service.user.IUserRegistService;
import business.service.user.impl.UserRegistService;

import com.lab107team.R;


@SuppressLint("HandlerLeak")
public class RegisterActivity extends Activity implements OnClickListener {
	private static final String TAG = RegisterActivity.class.getSimpleName();

	private static final int REGIST_OK = 1;
	private static final int REGIST_NOT_OK = 0;

	private ProgressDialog pdWait;

	private Dialog dlgSuccess;
	private Dialog dlgFail;

	private UserRegisterViewModel model;
	private IUserRegistService service;

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			pdWait.dismiss();
			if (msg.what == REGIST_OK) {
				Log.d(TAG, "Register Success");
				dlgSuccess.show();
			} else if (msg.what == REGIST_NOT_OK) {
				Log.d(TAG, "Register Fail");
				String errMsg = (String) msg.obj;
				Toast.makeText(RegisterActivity.this, errMsg, Toast.LENGTH_SHORT).show();
				dlgFail.show();
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register_activity_layout);

		model = new UserRegisterViewModel();
		service = new UserRegistService(model);

		initControlsAndRegEvent();
	}

	protected void initControlsAndRegEvent() {
		((ImageButton) findViewById(R.id.btn_regist_user_go_back)).setOnClickListener(this);
		((Button) findViewById(R.id.btn_regist_user_regist)).setOnClickListener(this);
		
		initProgressDialog();

		dlgSuccess = createDialog("注册成功！", new SuccessBtnClickListener());
		dlgFail = createDialog("注册失败!", new FailBtnClickListener());
	}

	private void initProgressDialog() {
		pdWait = new ProgressDialog(this);
		pdWait.setTitle("请稍候..");
		pdWait.setMessage("正在验证..");
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_regist_user_go_back:
			onBackButtonClick();
			break;

		case R.id.btn_regist_user_regist:
			onRegisterButtonClick();
			break;

		default:
			break;
		}
	}

	private void onBackButtonClick() {
		finish();
	}

	private void onRegisterButtonClick() {
		final Message msg = new Message();
		msg.what = REGIST_NOT_OK;
		if (!WebServiceHelper.isconnected(RegisterActivity.this)) {
			msg.obj = "请检查网络";
			handler.sendMessage(msg);
			return;
		}

		String account = ((EditText) findViewById(R.id.et_regist_user_account)).getText().toString().trim();
		String password = ((EditText) findViewById(R.id.et_regist_user_password)).getText().toString().trim();
		String rePassword = ((EditText) findViewById(R.id.et_regist_user_re_password)).getText().toString().trim();
		if (!password.equals(rePassword)) {
			msg.obj = "密码不一致";
			handler.sendMessage(msg);
			return;
		}
		pdWait.show();
		final User user = new User(account, password);
		new Thread(new Runnable() {

			@Override
			public void run() {
				boolean flag = service.register(user);
				if (flag) {
					Log.d(TAG, "注册成功");
					msg.what = REGIST_OK;
				} else {
					msg.what = REGIST_NOT_OK;
					msg.obj = (Object) "注册失败";
				}
				handler.sendMessage(msg);
			}
		}).start();
	}

	private Dialog createDialog(String title, DialogInterface.OnClickListener listener) {
		AlertDialog.Builder builder = new Builder(this);

		builder.setTitle("提示");
		builder.setMessage(title);

		builder.setPositiveButton("确认", listener);
		return builder.create();
	}

	private class SuccessBtnClickListener implements DialogInterface.OnClickListener {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dlgSuccess.dismiss();
			setResult(RESULT_OK);
			finish();
		}

	}

	private class FailBtnClickListener implements DialogInterface.OnClickListener {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dlgFail.dismiss();
		}

	}
}
